﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Xml;
using System.Reflection;
using System.Diagnostics;

namespace iPhoneUI.Shell
{
    public class ObjectConfig : Config
    {
        public ObjectConfig(XmlNode config)
            : base(config)
        {
        }

        public Type InstanceType
        {
            get
            {
                // first look for a direct type declaration
                string type = GetValue("@Type", string.Empty);

                // if that's no found look for an OfType declaration on the parent
                if (string.IsNullOrEmpty(type))
                {
                    XmlNode parentType = GetNode().SelectSingleNode("parent::node()/@OfType");
                    Debug.Assert(parentType != null);
                    type = parentType.InnerText;
                }

                return Type.GetType(type); 
            }
        }

        public object CreateInstance()
        {
            object instance = Activator.CreateInstance(InstanceType);
            instance.SetProperties(Properties);

            return instance;
        }
    }
}
